package com.woniuxy.rms.service.impl;

import com.woniuxy.rms.dao.*;
import com.woniuxy.rms.dto.CommentsDTO;
import com.woniuxy.rms.service.CommentService;
import com.woniuxy.rms.util.BusinessTypeUtil;
import com.woniuxy.rms.util.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Auther: ChenGang  <br/>
 * Date: 2020/3/26 0026:下午 6:35  <br/>
 * Description:评论服务实现
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CommentServiceImpl implements CommentService {

    @Autowired
    private DynamicCommentsMapper dynamicCommentsMapper;

    @Autowired
    private HotCommentsMapper hotCommentsMapper;

    @Autowired
    private GoodsCommentsMapper goodsCommentsMapper;

    @Autowired
    private ServerCommentsMapper serverCommentsMapper;

    @Autowired
    private GoodsCommentTagMapper goodsCommentTagMapper;

    @Override
    public List<CommentsDTO> findByPage(Integer type, Date startTime, Date endTime) {
        List<CommentsDTO> result = null;
        //动态评论
        if (type.equals(BusinessTypeUtil.DYNAMIC.getType())){
            result = dynamicCommentsMapper.search(startTime,endTime);
        }
        //服务评论
        if (type.equals(BusinessTypeUtil.SERVER.getType())){
            result = serverCommentsMapper.search(startTime,endTime);
        }
        //商品评论
        if (type.equals(BusinessTypeUtil.GOODS.getType())){
            //查询商品评论
            result = goodsCommentsMapper.search(startTime,endTime);
        }
        //活动评论
        if (type.equals(BusinessTypeUtil.HOT.getType())){
            result = hotCommentsMapper.search(startTime,endTime);
        }
        return result;
    }

    @Override
    public DataResult delById(Long id,Integer type) {
        //动态评论
        if (type.equals(BusinessTypeUtil.DYNAMIC.getType())){
            dynamicCommentsMapper.deleteByIdAndParentId(id);
        }
        //服务评论
        if (type.equals(BusinessTypeUtil.SERVER.getType())){
            serverCommentsMapper.deleteByPrimaryKey(id);
        }
        //商品评论
        if (type.equals(BusinessTypeUtil.GOODS.getType())){
            //删除评论点赞
            goodsCommentTagMapper.deleteByCommentId(id);
            //删除评论
            goodsCommentsMapper.deleteByIdAndParentId(id);
        }
        //活动评论
        if (type.equals(BusinessTypeUtil.HOT.getType())){
            hotCommentsMapper.deleteByIdAndParentId(id);
        }

        return DataResult.success();
    }

    @Override
    public DataResult batchDelByIds(String ids, Integer type) {
        //转换id串
        List<Long> commentIds = Arrays.stream(ids.split(",")).map(s -> Long.parseLong(s)).collect(Collectors.toList());
        //动态评论
        if (type.equals(BusinessTypeUtil.DYNAMIC.getType())){
            dynamicCommentsMapper.deleteByIdAndParentIds(commentIds);
        }
        //服务评论
        if (type.equals(BusinessTypeUtil.SERVER.getType())){
            serverCommentsMapper.deleteByIds(commentIds);
        }
        //商品评论
        if (type.equals(BusinessTypeUtil.GOODS.getType())){
            //删除评论点赞
            goodsCommentTagMapper.deleteByCommentIds(commentIds);
            //删除评论
            goodsCommentsMapper.deleteByIdAndParentIds(commentIds);

        }
        //活动评论
        if (type.equals(BusinessTypeUtil.HOT.getType())){
            hotCommentsMapper.deleteByIdAndParentIds(commentIds);
        }
        return DataResult.success();
    }

}